package com.tcr.main;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.Time;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.tcr.entities.*;
import com.tcr.main.R;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.json.JSONObject;

/**
 * A EFFACER
 */
/*public class Main extends Activity {
    //On déclare toutes les variables dont on aura besoin

    private final String URLTCR = "http://10.0.2.2:8080/TheCityRater-war/services/";
    private HttpClient httpClient;
    private HttpPost httpPost;
    private HttpPut httpPut;
    private HttpGet httpGet;
    private HttpDelete httpDelete;
    private Button listeEnseignes;
    private Button creerUser;
    private Button modifUser;
    private Button effacerUser;
    private Button creerEnseigne;
    private Button modifEnseigne;
    private Button effacerEnseigne;
    private ObjectMapper objectMapper = null;
    private ArrayList<Enseigne> enseignes;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //On récupère tous les éléments de notre interface graphique grâce aux ID
        listeEnseignes = (Button) findViewById(R.id.listeEnseigne);
        creerUser = (Button) findViewById(R.id.creerUser);
        modifUser = (Button) findViewById(R.id.modifierUser);
        effacerUser = (Button) findViewById(R.id.effacerUser);

        creerEnseigne = (Button) findViewById(R.id.creerEnseigne);
        modifEnseigne = (Button) findViewById(R.id.modifierEnseigne);
        effacerEnseigne = (Button) findViewById(R.id.effacerEnseigne);

        listeEnseignes.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                enseignes = recuperationListeE("enseignes");
                if (enseignes != null) {
                    Bundle objetbunble = new Bundle();
                    for (int i = 0; i < enseignes.size(); i++) {
                    //    objetbunble.putString(enseignes.get(i).getNom(), enseignes.get(i).getDescription());
                    }
                   // Intent intent = new Intent(Main.this, GetAllEnseigneActivity.class);
                    ///intent.putExtras(objetbunble);
                    //startActivity(intent);
                }
            }
        });

        creerUser.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                ajout("utilisateurs");
            }
        });

        modifUser.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                modification("utilisateurs");
            }
        });

        effacerUser.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                suppression("utilisateurs/5");
            }
        });

        creerEnseigne.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                ajout("enseignes");
            }
        });

        modifEnseigne.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                modification("enseignes");
            }
        });

        effacerEnseigne.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                suppression("enseignes/1");
            }
        });


    }

    public InputStream get(String url) {

        try {

            httpClient = new DefaultHttpClient();

            httpGet = new HttpGet(URLTCR + url);

            HttpResponse response;

            response = httpClient.execute(httpGet);

            Log.i("GET", response.getStatusLine().toString());


            HttpEntity entity = response.getEntity();
            InputStream input = entity.getContent();
            return input;
        } catch (IOException ex) {
            System.err.print("Erreur GET: " + ex.getLocalizedMessage());
            return null;
        }


    }

    public ArrayList recuperationListeE(String url) {
        InputStream input = get("enseignes");
        if (input == null) {
            System.err.println("Input erreur recuperation enseigne");
            return null;
        }
        ArrayList<Enseigne> res = new ArrayList<Enseigne>();
        try {
            // Utilisatation du package Jackson pour JSon
            objectMapper = new ObjectMapper();
            res = objectMapper.readValue(input, new TypeReference<ArrayList<Restaurant>>() {
            });
            return res;
        } catch (Exception ex) {
            String s = "Erreur android " + ex.getLocalizedMessage();

            s = "test";
            return null;
        }

    }

    // On veut ajouter un nouvel utilisateur ou une nouvelle enseigne
    public void ajout(String url) {
        try {

            httpClient = new DefaultHttpClient();
            httpPost = new HttpPost(URLTCR + url);

            // On créer un objet JSON qui sera transmis au serveur
            JSONObject jsonObject = new JSONObject();
            if (url != null) {
                if (url.equalsIgnoreCase("utilisateurs")) {
                    jsonObject.put("LOGIN", "blabla");
                    jsonObject.put("MDP", "blabla");
                    jsonObject.put("NOM", "blabla");
                    jsonObject.put("PRENOM", "blabla");
                    jsonObject.put("MAIL", "blabla");
                    jsonObject.put("TELEPHONE", "blabla");
                } else if (url.equalsIgnoreCase("enseignes")) {
                    jsonObject.put("NOM", "blabla");
                    jsonObject.put("DESCRIPTION", "blabla");
                    jsonObject.put("ADRESSE", "blabla");
                    jsonObject.put("VILLE", "blabla");
                    jsonObject.put("TYPE", "Restaurant");
                }
            } else {
                throw new Exception("URL nulle");
            }

            // On transforme le JSON afin de pouvoir transmettre les informations
            StringEntity stringEntity = new StringEntity(jsonObject.toString());
            stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
            httpPost.setHeader("Accept", "application/json");
            httpPost.setHeader("Content-Type", "application/json");
            httpPost.setEntity(stringEntity);
            HttpResponse response;

            response = httpClient.execute(httpPost);

            Log.i("Ajout", response.getStatusLine().toString());


        } catch (Exception ex) {
            System.err.print("Erreur POST: " + ex.getLocalizedMessage());
        }
    }

    public void modification(String url) {
        try {
            httpClient = new DefaultHttpClient();
            httpPut = new HttpPut(URLTCR + url);

            // On créer un objet JSON qui sera transmis au serveur
            JSONObject jsonObject = new JSONObject();
            if (url != null) {
                if (url.equalsIgnoreCase("utilisateurs")) {
                    jsonObject.put("ID", "5");
                    jsonObject.put("MDP", "test");
                    jsonObject.put("NOM", "test");
                    jsonObject.put("PRENOM", "test");
                    jsonObject.put("MAIL", "test");
                    jsonObject.put("TELEPHONE", "test");
                } else if (url.equalsIgnoreCase("enseignes")) {
                    jsonObject.put("ID", "1");
                    jsonObject.put("NOM", "test");
                    jsonObject.put("DESCRIPTION", "test");
                    jsonObject.put("ADRESSE", "test");
                    jsonObject.put("VILLE", "test");
                    jsonObject.put("TYPE", "Restaurant");
                }
            } else {
                throw new Exception("URL nulle");
            }

            // On transforme le JSON afin de pouvoir transmettre les informations
            StringEntity stringEntity = new StringEntity(jsonObject.toString());
            stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
            httpPut.setHeader("Accept", "application/json");
            httpPut.setHeader("Content-Type", "application/json");
            httpPut.setEntity(stringEntity);
            HttpResponse response;

            response = httpClient.execute(httpPut);
        } catch (Exception ex) {
            System.err.print("Erreur Put: " + ex.getLocalizedMessage());
        }

    }

    public void suppression(String url) {
        try {
            httpClient = new DefaultHttpClient();
            httpDelete = new HttpDelete(URLTCR + url);

            HttpResponse response;

            response = httpClient.execute(httpDelete);

        } catch (Exception ex) {
            System.err.print("Erreur Delete: " + ex.getLocalizedMessage());
        }
    }
    
}*/